package com.iplant.basic.controller;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.base.iPlantRequestBody;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
public class BasicController<T extends BasePO> extends BaseController {


	protected  Class<T> mClazz;

	protected PoService<T> mPoService;

	public BasicController(Class<T> wClazz){
		mClazz=wClazz;
		mPoService= ORMUtils.GetPoService(wClazz);
	}

	/**
	 * 查单条
	 */
	@TraceAPI( value = "单条查询")
	@GetMapping("/Info")
	public Object Info(HttpServletRequest request) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			Map<String, Object> wParams = GetParamsMap(request);
			ServiceResult<T> wServiceResult = mPoService.SelectPO(wLoginUser, wParams);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 条件查询
	 */
	@TraceAPI("批量查询")
	@GetMapping("/All")
	public Object All(HttpServletRequest request) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			Map<String, Object> wParams = GetParamsMap(request);
			Pagination wPagination = GetPagination(request,mClazz);

			ServiceResult<List<T>> wServiceResult =
					mPoService.SelectPOList(wLoginUser, wParams, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 新增或更新
	 */
	@TraceAPI(value = "新增或更新")
	@PostMapping("/Update")
	public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<T> wServiceResult = mPoService.UpdateSelectPo(wLoginUser, wParam);


			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 新增或更新
	 */
	@TraceAPI(value = "新增或更新")
	@PostMapping("/UpdateT")
	public Object UpdateT(HttpServletRequest request, @RequestBody iPlantRequestBody<T> wBody) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<T> wServiceResult = mPoService.UpdatePo(wLoginUser, wBody);


			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 新增或更新
	 */
	@TraceAPI("批量新增或更新")
	@PostMapping("/UpdateList")
	public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<List<T>> wServiceResult = mPoService.UpdatePoList(wLoginUser, wParam);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 新增或更新
	 */
	@TraceAPI("批量新增或更新")
	@PostMapping("/UpdateTList")
	public Object UpdateTList(HttpServletRequest request, iPlantRequestBody<List<T>> wBody) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<List<T>> wServiceResult = mPoService.UpdatePoList(wLoginUser, wBody);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 批量激活或禁用
	 */
	@TraceAPI("激活")
	@PostMapping("/Active")
	public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<Integer> wServiceResult = mPoService.ActivePOList(wLoginUser, wParam);

			if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	/**
	 * 批量删除
	 */
	@TraceAPI("批量删除")
	@PostMapping("/DeleteList")
	public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<Integer> wServiceResult = mPoService.DeletePOList(wLoginUser, wParam);

			if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@TraceAPI("删除")
	@PostMapping("/Delete")
	public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<Integer> wServiceResult = mPoService.DeletePO(wLoginUser, wParam);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@TraceAPI("激活变更")
	@PostMapping("/ActiveChange")
	public Object ActiveChange(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			ServiceResult<Integer> wServiceResult = mPoService.ActivePOChange(wLoginUser, wParam);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}
}
